home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / tuner / tuner_sl.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  123 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Socket-related subroutines shared by broadcast and radio */
  26.  
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <stdlib.h>
  30. #include <fcntl.h>
  31. #include <netdb.h>
  32.  
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <sys/time.h>
  36. #include <netinet/in.h>
  37.  
  38. #include "tuner.h"
  39.  
  40. #define MSG_BUFSIZE 128                /* Message size */
  41.  
  42. int
  43. opensock()
  44. {
  45.     int s;
  46.     int on = 1;
  47.  
  48.     s = socket(AF_INET, SOCK_DGRAM, 0);
  49.     if (s < 0) {
  50.         perror("socket()");
  51.         exit(1);
  52.     }
  53.  
  54.     if (setsockopt(s, SOL_SOCKET, SO_BROADCAST,
  55.             &on, sizeof (on)) < 0) {
  56.         perror("setsockopt(SO_BROADCAST)");
  57.         exit(1);
  58.     }
  59.     return s;
  60. }
  61.  
  62. int
  63. openscsock()
  64. {
  65.     int s;
  66.     int on = 1;
  67.     struct sockaddr_in sin;
  68.  
  69.     s = socket(AF_INET, SOCK_DGRAM, 0);
  70. #ifdef SO_REUSEPORT
  71.     (void)setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on));
  72. #endif
  73.     bzero((char *)&sin, sizeof sin);
  74.     sin.sin_addr.s_addr = INADDR_ANY;
  75.     sin.sin_family = AF_INET;
  76.     sin.sin_port = htons(CTLPRT_SCALL);
  77.     if ( bind(s, &sin, sizeof sin) < 0) {
  78.     perror("bind(stationcallsocket)");
  79.     exit(1);
  80.     }
  81.     return s;
  82. }
  83.  
  84. char *
  85. recvsock(s)
  86.     int s;
  87. {
  88.     fd_set fdset;
  89.     int rv;
  90.     struct timeval tv;
  91.     static char buf[MSG_BUFSIZE];
  92.  
  93.     FD_ZERO(&fdset);
  94.     FD_SET(s, &fdset);
  95.     tv.tv_sec = WAIT_TIMEOUT;
  96.     tv.tv_usec = 0;
  97.     if( (rv = select(s+1, &fdset, (fd_set *)0, (fd_set *)0, &tv)) < 0 ) {
  98.     perror("select(socket)");
  99.     exit(1);
  100.     }
  101.     if ( rv == 0 )
  102.       return NULL;
  103.     if ( (rv=recv(s, buf, sizeof(buf), 0)) < 0 ) {
  104.     perror("recv()");
  105.     exit(1);
  106.     }
  107.     buf[rv] = 0;
  108.     return buf;
  109. }
  110.   
  111.  
  112. void
  113. sendsock(s, to, msg)
  114.     int s;
  115.     struct sockaddr_in *to;
  116.     char *msg;
  117. {
  118.     if ( sendto(s, msg, strlen(msg), 0, to, sizeof(*to)) < 0 ) {
  119.     perror("sendto()");
  120.     exit(1);
  121.     }
  122. }
  123.